import React, { Component } from 'react';
import { connect } from 'dva';
import { routerRedux } from 'dva/router';

class Clock extends Component {
    constructor(props) {
        super(props);
        this.state = {
            times: 0
        }
        
    }
    clock(action) {
        let time = this.state.times;
        const data = this.props.cardData;
        global.timer1 = setInterval((e) => {
            time--;
            if (time < 0) {
                clearInterval(global.timer1);
                this.setState({
                    times: 60
                });
                // this.props.dispatch({
                //     type: 'clock/change',
                //     payload: {
                //         visible: false
                //     }
                // })
                // this.props.dispatch({
                //     type: 'cardProcess/clear',
                //     payload: {
                //         data: null
                //     }
                // })
                // this.props.dispatch({
                //     type: 'getCard/clear',
                //     payload: {
                //         data: null
                //     }
                // })
                console.log(data.postDataResult)
                if(data.postDataResult === '0'){
                    // 完成操作   发卡
                    this.props.dispatch({
                        type: 'cardProcess/sendCard',
                    })
                }
                this.props.dispatch(routerRedux.push('/'));
                return false;
            }
            this.setState({
                times: time
            });
        }, 1000);
    }
    componentWillMount(){
        this.setState({ times:60 });
        global.clock = this.clock.bind(this)
    }

    componentWillUnmount(){
        clearInterval(global.timer1);
        this.props.dispatch({
            type: 'clock/change',
            payload: {
                visible: false
            }
        })
    }
    render() {
        const { data } = this.props;
        return (
            <div>
                {
                    data.visible ?
                        <div style={{ width: 74, height: 74, borderRadius: 37, textAlign: 'center', lineHeight: "74px", fontSize: '46px', color: '#fff' }}>
                            {this.state.times}
                        </div>
                        :
                        ''
                }
            </div>
        )
    }
}

function mapStateToProps(state) {
    const data = state.clock;
    const cardData = state.cardProcess;
    return { data,cardData };
}
export default connect(mapStateToProps)(Clock);